import { Field, Int, ObjectType } from "@nestjs/graphql";
import { ClassType } from "type-graphql";
import { Page } from "./page.type";
/**
 * 返回数据的接口规范：
 * {
 *   code: 200, // 状态码
 *   data: [], // 返回数据
 *   message: '成功', // 信息
 *   page: {
 *     start: 0, // 开始页码
 *     length: 20, // 每页数量
 *     total: 100, // 总数
 *   },
 *   debug: '',
 *   key: ''
 * }
 */

export interface IResult<T> {
  code: number;
  message: string;
  data?: T;
}

export interface IResults<T> {
  code: number;
  message: string;
  data?: T[];
  page?: Page;
}

@ObjectType()
export class Result {
  @Field(() => Int)
  code: number;
  @Field(() => String)
  message: string;
  @Field(() => String, { nullable: true })
  data?: string;
}

// ClassType这个类型通常用于泛型上下文中，以提供对类类型的引用，而不是实例类型
export function createResult<T extends object>(ItemType: ClassType<T>): ClassType<IResult<T>> {
  @ObjectType()
  class Result {
    @Field(() => Int)
    code: number;
    @Field(() => String)
    message: string;
    @Field(() => ItemType, { nullable: true })
    data?: T;
  }
  return Result;
}

export function createResults<T extends object>(ItemType: ClassType<T>): ClassType<IResults<T>> {
  @ObjectType()
  class Results {
    @Field(() => Int)
    code: number;
    @Field(() => String)
    message: string;
    @Field(() => [ItemType], { nullable: true })
    data?: T[];
    @Field(() => Page, { nullable: true })
    page?: Page;
  }
  return Results;
}
